package com.qf.homeproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.qf.fragment.ContentFragment_shouye;
import com.qf.fragment.FindFragment;
import com.qf.fragment.MessageFragment;
import com.qf.fragment.MineFragment;

public class MainActivity extends FragmentActivity
{
	private TextView tView;
	private RadioGroup rGroup;
	String[] title = { "首页", "发现", "消息", "我的" };
	private EditText edit;
	private Button saoBtn;
	private TextView cityName;
	private TextView search_city;
	private TextView search_shouyeid;
	private String cityId;
	private String fromIndexCityName;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		initSelectedEvent();
		// 默认上来之后执行首页碎片的内容
		loadFragment(R.id.indexId);
		toCityList();
	}

	private void toCityList()
	{
		cityName.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// 跳转到城市列表页面
				Intent intent = new Intent(MainActivity.this,
						IndexActivity.class);
				startActivity(intent);

				// 跳转Activity时开启动画(转场动画)
				overridePendingTransition(R.anim.citylist_slide_in,
						R.anim.slide_out_bottom);

			}
		});
	}

	private void initSelectedEvent()
	{
		rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				Log.i("info", "被选中的按钮为:" + checkedId);
				for (int i = 0; i < rGroup.getChildCount(); i++)
				{

					if (group.getChildAt(i).getId() == checkedId && i >= 1)
					{

						tView.setText(title[i]);
					}

				}
				loadFragment(checkedId);
			}
		});
	}

	private void loadFragment(int checkedId)
	{
		switch (checkedId)
		{
			case R.id.indexId:
				Fragment conFrag = ContentFragment_shouye.newInstance(cityId);
				switchFragment(conFrag);
				indexShowAndShow();
				break;
			case R.id.foundId:
				// 同时隐藏首页面的图片
				Fragment findFrag = FindFragment.newInstance("");
				switchFragment(findFrag);
				visibleAndShow();
				break;
			case R.id.messageId:
				Fragment msgFrag = MessageFragment.newInstance("");
				switchFragment(msgFrag);
				visibleAndShow();
				break;
			case R.id.mineId:
				Fragment mineFrag = MineFragment.newInstance("");
				switchFragment(mineFrag);
				visibleAndShow();
				break;

		}

	}

	private void indexShowAndShow()
	{
		saoBtn.setVisibility(View.VISIBLE);
		cityName.setVisibility(View.VISIBLE);
		tView.setVisibility(View.INVISIBLE);
		search_shouyeid.setVisibility(View.VISIBLE);
	}

	private void visibleAndShow()
	{
		// edit.setVisibility(View.INVISIBLE);
		saoBtn.setVisibility(View.INVISIBLE);
		cityName.setVisibility(View.INVISIBLE);
		search_shouyeid.setVisibility(View.INVISIBLE);
		tView.setVisibility(View.VISIBLE);
	}

	private void switchFragment(Fragment fragment)
	{
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.containerId, fragment).commit();
	}

	private void init()
	{

		Intent intent = getIntent();
		cityId = intent.getStringExtra("cityId");
		fromIndexCityName = intent.getStringExtra("cityName");

		tView = (TextView) findViewById(R.id.title_barId);
		cityName = (TextView) findViewById(R.id.shouye_cityname);
		// search_city = (TextView) findViewById(R.id.shouye_Id);
		search_shouyeid = (TextView) findViewById(R.id.search_shouye_Id);
		rGroup = (RadioGroup) findViewById(R.id.rGroupId);
		edit = (EditText) findViewById(R.id.title_baredti_Id);
		saoBtn = (Button) findViewById(R.id.btn_saoyisao);
		// 默认首页被选中
		rGroup.check(R.id.indexId);
		tView.setText(title[0]);
		cityName.setText(fromIndexCityName);

		toSearchPage(cityId);
	}

	/**
	 * 转到搜索页面
	 */

	private void toSearchPage(final String cityId)
	{
		search_shouyeid.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// 跳转到搜索页面
				Intent intent = new Intent(MainActivity.this,
						SearchActivity.class);
				intent.putExtra("cityId", cityId);
				intent.putExtra("cityName", fromIndexCityName);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_bottom,
						R.anim.slide_out_bottom);
			}
		});
	}

	/**
	 * 对于返回键的处理的事件(点击两次退出程序)
	 */
	long firstClick = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK) // 如果是返回键的话
		{
			long secondClick = System.currentTimeMillis();
			if (secondClick - firstClick > 2000)
			{
				Toast.makeText(MainActivity.this, "再次点击退出应用程序",
						Toast.LENGTH_SHORT).show();
				firstClick = secondClick;
				return true;
			}
			else
			{
				finish();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event); // 对返回事件不做任何处理
	}

}
